﻿/*
 * @file: MessageDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for MessageDA
/// </summary>
namespace DAL
{
    public class MessageDA : BaseDAL, IMessage
    {
        private const string tableName = "Messages";
        private const string messageID = "MessageID";
        private const string occasionID = "OccasionID";
        private const string messageContent = "MessageContent";
        private const string isEnable = "IsEnable";

        public MessageDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static int ContentExisted(string _messageContent) {
            try
            {
                return RecordExisted(tableName, messageContent, _messageContent);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region IMessage Members

        /*
         * @method: GetMessages
         * @description: get all Messages
         * @input: allowFilter, TRUE enables ignore hidden records
         * @output: all records corresponding
         */
        public Message[] GetMessages(bool allowFilter)
        {
            Message[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = 1",
                        tableName, isEnable);
                else
                    cmd.CommandText = String.Format("SELECT * FROM {0}", tableName);

                String[] columnNames = {    messageID, 
                                            occasionID,
                                            messageContent,
                                            isEnable};
                result = SelectCollection<Message>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetMessageByOccasionID
         * @description: get all Message records by OccasionID
         * @input: OccasionID, allowFilter <TRUE enables ignore hidden records>
         * @output: all records corresponding
         */
        public Message[] GetMessageByOccasionID(int _occasionID, bool allowFilter)
        {
            Message[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, occasionID, occasionID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", occasionID), SqlDbType.Int).Value = _occasionID;
                }
                else
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, occasionID, occasionID);
                    cmd.Parameters.Add(String.Format("@{0}", occasionID), SqlDbType.Int).Value = _occasionID;
                }

                String[] columnNames = {    messageID, 
                                            occasionID,
                                            messageContent,
                                            isEnable};
                result = SelectCollection<Message>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetMessageByMessageID
         * @description: get Message records by MessageID
         * @input: MessageID, allowFilter <TRUE enables ignore hidden records>
         * @output: record corresponding
         */
        public Message GetMessageByMessageID(int _messageID, bool allowFilter) {
            Message[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                if (allowFilter)
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2} AND {3} = 1",
                        tableName, messageID, messageID, isEnable);
                    cmd.Parameters.Add(String.Format("@{0}", messageID), SqlDbType.Int).Value = _messageID;
                }
                else
                {
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, messageID, messageID);
                    cmd.Parameters.Add(String.Format("@{0}", messageID), SqlDbType.Int).Value = _messageID;
                }

                String[] columnNames = {    messageID, 
                                            occasionID,
                                            messageContent,
                                            isEnable};
                result = SelectCollection<Message>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        /*
         * @method: InsertMessage
         * @description: insert new record to Message table
         * @input: Message entity
         * @output: 1 if insert successful, 0 if error occurred
         */
        public int InsertMessage(Message _message, out int autoNumber)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    occasionID,
                                            messageContent};

                Object[] values = { _message.OccasionID,
                                    _message.MessageContent};
              
                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: DisableMessage
         * @description: hidden a message
         * @input: MessageID, isEnable<TRUE if enable, FALSE if hidden>
         * @output: 1 if successful, 0 if error occurred
         */
        public int DisableMessage(int _messageID, bool _isEnable)
        {
            int result = 0;
            try
            {
                String[] columnNames = { isEnable };
                Object[] values = { _isEnable };
                String[] keyColumnNames = { messageID };
                Object[] keyColumnValues = { _messageID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: updateMessage
         * @description: update Message information
         * @input: Message entity
         * @output: 1 if update successful, 0 if error occurred
         */
        public int UpdateMessage(Message _message)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    occasionID,
                                            messageContent};
                Object[] values = { _message.OccasionID,
                                    _message.MessageContent};
                String[] keyColumnNames = { messageID };
                Object[] keyColumnValues = { _message.MessageID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}
